package com.woniuxy.dr_mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.dr_mall.entity.DrManager;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
public interface DrManagerMapper extends BaseMapper<DrManager> {
    /**
     * 添加管理员
     * @param drManager
     */
    @Insert("insert into dr_manager(id,account,password,errorCount,status) values(#{id},#{account},#{password},#{errorCount},#{status})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void add(DrManager drManager);

    /**
     * 通过账号查询管理员
     * @param account
     * @return
     */
    @Select("select *from dr_manager where account=#{account}")
    DrManager getByAccount(String account);
    /**
     * 通过ID恢复管理员状态
     * @param id
     * @param status
     * @return
     */
    @Update("update dr_manager set status='y' where id=#{id}")
    void getNormal(Integer id);
    /**
     * 通过ID禁用管理员状态
     * @param id
     * @param status
     * @return
     */
    @Update("update dr_manager set status='n' where id=#{id}")
    void getUnNormal(Integer id);
    /**
     * 查询全部管理员的权限和菜单
     * @return
     */
    @Select("select * from dr_manager ")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "perms",many = @Many(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrPermMapper.getPermsByDrManagerId"
            )),
            @Result(column = "id",property = "menus",many = @Many(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrMenuMapper.getMenusByDrManagerId"
            ))
    })
    List<DrManager> getDrManagers();

    /**
     *
     * @param id
     * @return
     */
    @Select("select * from dr_manager where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "perms",many = @Many(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrPermMapper.getPermsByDrManagerId"
            )),
            @Result(column = "id",property = "menus",many = @Many(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrMenuMapper.getMenusByDrManagerId"
            ))
    })
    DrManager getById(Integer id);

}
